// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   RoleServiceImp.java

package com.iphaeton.lms.business.role;

import com.iphaeton.common.helper.Pagination;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.exception.PersistenceException;
import com.iphaeton.lms.business.PersistentServiceSupport;
import com.iphaeton.lms.pojo.organize.User;
import com.iphaeton.lms.pojo.role.Privilege;
import com.iphaeton.lms.pojo.role.Role;
import com.iphaeton.persistence.IPersistenceService;
import java.util.*;
import org.apache.log4j.Logger;

// Referenced classes of package com.iphaeton.lms.business.role:
//            IRoleService

public class RoleServiceImp extends PersistentServiceSupport
    implements IRoleService
{

    public RoleServiceImp()
    {
    }

    public Role save(Role role)
        throws BusinessException
    {
        Role r = null;
        try
        {
            r = (Role)hbpservice.store(role);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't save role  '" + role.getName() + "'!");
        }
        return r;
    }

    public Privilege addPrivilege(Role r, Privilege p)
        throws BusinessException
    {
        Set role = p.getRole();
        if(role == null)
            role = new HashSet();
        role.add(r);
        p.setRole(role);
        try
        {
            p = (Privilege)hbpservice.store(p);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6DFB\u52A0\u6743\u9650\u5931\u8D25!");
        }
        return p;
    }

    public void remove(Role role)
        throws BusinessException
    {
        try
        {
            hbpservice.remove(role);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't remove role  '" + role.getName() + "'!");
        }
    }

    public Role findSystemAdmin()
        throws BusinessException
    {
        String hql = "from Role r where r.name='\u7CFB\u7EDF\u7BA1\u7406\u5458' and r.creater='system'";
        Role r = null;
        try
        {
            r = (Role)hbpservice.query(hql).get(0);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u627E\u4E0D\u5230'\u7CFB\u7EDF\u7BA1\u7406\u5458'\u89D2\u8272!");
        }
        return r;
    }

    public Role findStudyAdmin()
        throws BusinessException
    {
        String hql = "from Role r where r.name='\u5B66\u4E60\u7BA1\u7406\u5458' and r.creater='system'";
        Role r = null;
        try
        {
            r = (Role)hbpservice.query(hql).get(0);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u627E\u4E0D\u5230'\u5B66\u4E60\u7BA1\u7406\u5458'\u89D2\u8272!");
        }
        return r;
    }

    public Role findTeacher()
        throws BusinessException
    {
        String hql = "from Role r where r.name='\u6559\u5E08' and r.creater='system'";
        Role r = null;
        try
        {
            r = (Role)hbpservice.query(hql).get(0);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u627E\u4E0D\u5230'\u6559\u5E08'\u89D2\u8272!");
        }
        return r;
    }

    public Role findStudent()
        throws BusinessException
    {
        String hql = "from Role r where r.name='\u5B66\u5458' and r.creater='system'";
        Role r = null;
        try
        {
            r = (Role)hbpservice.query(hql).get(0);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u627E\u4E0D\u5230'\u5B66\u5458'\u89D2\u8272!");
        }
        return r;
    }

    public Role findRoleById(String id)
        throws BusinessException
    {
        if(id == null)
            throw new BusinessException("null id isn't allowed in function findRoleById(String id)!");
        Role role = null;
        try
        {
            role = (Role)hbpservice.get(id, com.iphaeton.lms.pojo.role.Role.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find role by id: " + id + "!");
        }
        return role;
    }

    public void setRole(Role role, User user)
        throws BusinessException
    {
        Set u = role.getUser();
        if(u == null)
            u = new HashSet();
        u.add(user);
        role.setUser(u);
        try
        {
            hbpservice.store(role);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6DFB\u52A0\u89D2\u8272\u5931\u8D25!");
        }
    }

    public Pagination findRolePagination(String userId, String row, int pageSize)
        throws BusinessException
    {
        String selectHql = "select count(r) from Role r where r.creater='" + userId + "' or r.creater='system'";
        String hqlQueryStr = "from Role r where r.creater='" + userId + "' or r.creater='system'";
        if("".equals(row))
            hqlQueryStr = hqlQueryStr + " order by r.name";
        else
            hqlQueryStr = hqlQueryStr + " order by r." + row;
        List list = null;
        try
        {
            list = hbpservice.query(selectHql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find Role !");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(hqlQueryStr);
        return pageInfo;
    }

    public Pagination findUserPagination(String roleId, int pageSize)
        throws BusinessException
    {
        if(roleId == null)
            logger.debug("^_^,it's just a passed into parameter for illustrating!");
        String selectHql = "select count(u) from User u where u.role.id='" + roleId + "'";
        String hqlQueryStr = "from User u where u.role.id='" + roleId + "' order by  u.name";
        List list = null;
        try
        {
            list = hbpservice.query(selectHql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find User !");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(hqlQueryStr);
        return pageInfo;
    }

    public Pagination findNotRoleUserPagination(String url, String roleId, String row, int pageSize)
        throws BusinessException
    {
        if(roleId == null || url == null)
            logger.debug("^_^,it's just a passed into parameter for illustrating!");
        String selectHql = "select count(u) from User u where u.organize.url like '" + url + "%' and u.id not in (select u.id from u where u.role.id='" + roleId + "' )";
        String hqlQueryStr = "select u from User u where u.organize.url like '" + url + "%' and u.id not in (select u.id from u where u.role.id='" + roleId + "' )order by " + row;
        List list = null;
        try
        {
            list = hbpservice.query(selectHql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find User !");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(hqlQueryStr);
        return pageInfo;
    }

    public void removePrivilege(Role r, Privilege p)
        throws BusinessException
    {
        if(p == null || r == null)
            throw new BusinessException("null value isn't allowed in function removePrivilege(Role r, Privilege p)!");
        Set privilege = r.getPrivilege();
        Set newPg = new HashSet();
        for(Iterator it = privilege.iterator(); it.hasNext();)
        {
            Privilege temp = (Privilege)it.next();
            if(!temp.equals(p))
                newPg.add(temp);
        }

        r.setPrivilege(newPg);
        try
        {
            hbpservice.store(r);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't remove Privilege:" + p.getName() + "from Role:" + r.getName() + "!");
        }
    }

    public void removeUser(Role r, String id)
        throws BusinessException
    {
        if(id == null || r == null)
            throw new BusinessException("null value isn't allowed in function removeUser(Role r, String id)!");
        Set user = r.getUser();
        Set newUser = new HashSet();
        for(Iterator it = user.iterator(); it.hasNext();)
        {
            User temp = (User)it.next();
            if(!temp.getId().equals(id))
                newUser.add(temp);
        }

        r.setUser(newUser);
        try
        {
            hbpservice.store(r);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't remove user by id:" + id + "from Role:" + r.getName() + "!");
        }
    }

    public List selectRole(String userId)
        throws BusinessException
    {
        List list = null;
        String hql = "from Role r where r.creater='" + userId + "' or r.creater='system'";
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u627E\u5F53\u524D\u7CFB\u7EDF\u89D2\u8272\u5217\u8868\u51FA\u9519!");
        }
        return list;
    }

    private static Logger logger;
    static Class class$0; /* synthetic field */
    static Class class$1; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.business.role.RoleServiceImp.class.getName());
    }
}
